package com.smart.stock.module.spider.jobSpiderApi;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.smart.stock.common.util.stock.StockSpider;
import com.smart.stock.module.spider.entity.WebPage;
import com.smart.stock.common.util.DateUtils;
import com.smart.stock.module.GlobalConstant;
import com.smart.stock.module.spider.util.JsoupUitl;
import com.smart.stock.module.spider.util.RequestUtil;
import com.smart.stock.module.spider.util.TradingDateUtil;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Connection;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.time.LocalDateTime;
import java.util.*;

/**
 *
 * @author chenguoxiang
 * @create 2019-11-13 11:21
 **/
@Slf4j
@Component
public class XueQiuApi {
    @Value("${xueqiu.token}")
    private String token;
    //股东人数变化
    private String holders_url= GlobalConstant.URL_XUEQIU_HOLDERS;
    private Map<String, String> cookie;
    private TradingDateUtil tradingDateUtil;

    @PostConstruct
    private void init(){
        this.cookie = new HashMap<>();
        this.cookie.put("xq_a_token", token);
        this.tradingDateUtil = new TradingDateUtil();
    }

    /**
     * 得到股东人数变化
     * @param code 代码
     * @return
     */
    public JSONArray getHolders(String code){
        String url = String.format(holders_url, StockSpider.formatStockCode(code).toUpperCase());
        WebPage webPage =
                JsoupUitl.getWebPage(
                        url, Connection.Method.GET,
                        8000, null, "https://xueqiu.com/",
                        cookie, null,
                        true, true);
        JSONObject data = JSONObject.parseObject(webPage.getDocument().text());
        if(data.getInteger("error_code")==0){
            JSONArray rows = data.getJSONObject("data").getJSONArray("items");
            for (int i = 0; i <rows.size() ; i++) {
                String date=TradingDateUtil.long2string(rows.getJSONObject(i).getLong("timestamp"));
                rows.getJSONObject(i).put("timestamp",date);
            }
            return rows;
        }
        log.error("请求错误：{}",data.getString("error_description"));
        return null;
    }

    /**
     * 上证指数
     * 雪球行情
     * https://xueqiu.com/hq
     * @return
     */
    public List<JSONArray> getIndexSH(){
        String url = String.format(GlobalConstant.URL_INDEX_SH, DateUtils.toLong(LocalDateTime.now()));
        String str = GlobalConstant.XUEQIU_COOKIE_STR;
        Map<String, String> cookie = RequestUtil.string2token(str);
        WebPage webPage =
                JsoupUitl.getWebPage(
                        url, Connection.Method.GET,
                        8000, null, "https://xueqiu.com/",
                        cookie, null,
                        true, true);
        JSONObject datas = JSONObject.parseObject(webPage.getDocument().text());
        if(datas.getInteger("error_code")==0){
            JSONArray meta = datas.getJSONObject("data").getJSONArray("column");
            JSONArray data = datas.getJSONObject("data").getJSONArray("item");
            for (int i = 0; i <data.size() ; i++) {
                String date=TradingDateUtil.long2string(data.getJSONArray(i).getLong(0));
                data.getJSONArray(i).set(0,date);
            }
            return Arrays.asList(meta,data);
        } else {
            log.error("上证指数请求错误：{}",datas.getString("error_description"));
            return null;
        }

    }

    /**
     * 深证指数
     * @return
     */
    public List<JSONArray> getIndexSZ() {
        String url = String.format(GlobalConstant.URL_INDEX_SZ, DateUtils.toLong(LocalDateTime.now()));
//        String url = String.format(holders_url, StockSpider.formatStockCode(code).toUpperCase());
        String str = GlobalConstant.XUEQIU_COOKIE_STR;
        Map<String, String> cookie = RequestUtil.string2token(str);
        WebPage webPage =
                JsoupUitl.getWebPage(
                        url, Connection.Method.GET,
                        8000, null, "https://xueqiu.com/",
                        cookie, null,
                        true, true);
        JSONObject datas = JSONObject.parseObject(webPage.getDocument().text());
        if(datas.getInteger("error_code")==0){
            JSONArray meta = datas.getJSONObject("data").getJSONArray("column");
            JSONArray data = datas.getJSONObject("data").getJSONArray("item");
            for (int i = 0; i <data.size() ; i++) {
                String date=TradingDateUtil.long2string(data.getJSONArray(i).getLong(0));
                data.getJSONArray(i).set(0,date);
            }
            return Arrays.asList(meta,data);
        } else {
            log.error("上证指数请求错误：{}",datas.getString("error_description"));
            return null;
        }
    }

    /**
     * 创业板指数
     * @return
     */
    public List<JSONArray> getIndexCYB() {
        String url = String.format(GlobalConstant.URL_INDEX_CYB, DateUtils.toLong(LocalDateTime.now()));
//        String url = String.format(holders_url, StockSpider.formatStockCode(code).toUpperCase());
        String str = GlobalConstant.XUEQIU_COOKIE_STR;
        Map<String, String> cookie = RequestUtil.string2token(str);
        WebPage webPage =
                JsoupUitl.getWebPage(
                        url, Connection.Method.GET,
                        8000, null, "https://xueqiu.com/",
                        cookie, null,
                        true, true);
        JSONObject datas = JSONObject.parseObject(webPage.getDocument().text());
        if(datas.getInteger("error_code")==0){
            JSONArray meta = datas.getJSONObject("data").getJSONArray("column");
            JSONArray data = datas.getJSONObject("data").getJSONArray("item");
            for (int i = 0; i <data.size() ; i++) {
                String date=TradingDateUtil.long2string(data.getJSONArray(i).getLong(0));
                data.getJSONArray(i).set(0,date);
            }
            return Arrays.asList(meta,data);
        } else {
            log.error("上证指数请求错误：{}",datas.getString("error_description"));
            return null;
        }
    }
    /**
     *科创板指数
     * @return
     */
    public List<JSONArray> getIndexKCB() {
        String url = String.format(GlobalConstant.URL_INDEX_KCB, DateUtils.toLong(LocalDateTime.now()));
//        String url = String.format(holders_url, StockSpider.formatStockCode(code).toUpperCase());
        String str = GlobalConstant.XUEQIU_COOKIE_STR;
        Map<String, String> cookie = RequestUtil.string2token(str);
        WebPage webPage =
                JsoupUitl.getWebPage(
                        url, Connection.Method.GET,
                        8000, null, "https://xueqiu.com/",
                        cookie, null,
                        true, true);
        JSONObject datas = JSONObject.parseObject(webPage.getDocument().text());
        if(datas.getInteger("error_code")==0){
            JSONArray meta = datas.getJSONObject("data").getJSONArray("column");
            JSONArray data = datas.getJSONObject("data").getJSONArray("item");
            for (int i = 0; i <data.size() ; i++) {
                String date=TradingDateUtil.long2string(data.getJSONArray(i).getLong(0));
                data.getJSONArray(i).set(0,date);
            }
            return Arrays.asList(meta,data);
        } else {
            log.error("上证指数请求错误：{}",datas.getString("error_description"));
            return null;
        }
    }

    public static void main(String[] args) {
//        String str = "device_id=06104b2c525b670661f0cbb533ffa8da; s=ci12wr2sbo; xq_is_login=1; u=1686036676; xq_a_token=6c67243edfc0bee277e784e2666ef84afc748a74; xqat=6c67243edfc0bee277e784e2666ef84afc748a74; xq_id_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJ1aWQiOjE2ODYwMzY2NzYsImlzcyI6InVjIiwiZXhwIjoxNjI5Njc0OTA2LCJjdG0iOjE2MjcwODI5MDY3ODUsImNpZCI6ImQ5ZDBuNEFadXAifQ.AAnPrcojY4UUsRoxzQjmq20BafshkTbUo4wzl-NBnly3VAuzOwRw59Vo4Dq_-aasAaPhIr6XadS0W9TGfYJPNQldHwS0HjnPGAVx7Iej5AJnJ62R1F9pQHhWmHTNguAoBRn4w8g5O24UZPsEerUV2Lho0Gl8W-AvMqOTkEiu1G_xa8shMPOUnlPiVy0o0Iv3BpGyQbbmFHEy84go4_Bja1M6vSWuUTlVsahfKcGUCEJpwnEvNkHt_DwLbn37-GCKunmTUUzAM9xP1ZtqltUKTKlRkqAUY1lePI1rhLVBr6o14xV5ZJ5KNM6wxItkLHK9i2UK-WOmkXFEVkn4dOiXtQ; xq_r_token=700beac3205e50760562d85bd016f2100e4245bb; Hm_lvt_1db88642e346389874251b5a1eded6e3=1625029500,1625635847,1627082907; snbim_minify=true; bid=d6f78282805d3f3ee797c904353451b0_krgz5x5o; Hm_lpvt_1db88642e346389874251b5a1eded6e3=1627083125";
//        Map<String, String> cookie = RequestUtil.string2token(str);
//        System.out.println(cookie);
        XueQiuApi xueQiu = new XueQiuApi();
        System.out.println(xueQiu.getIndexKCB());
    }


}
